<template>
  <div class="shell-data">
    <div class="item flex">
      <span>销售量</span>
      <span class="unit">万元</span>
    </div>
    <div :key="nowCount" class="item flex">
      <div v-for="(item, index) in countString" :key="index" class="card">
        {{ item }}
      </div>
    </div>
    <div class="item">
      <span>完成百分比</span>
    </div>
    <div class="item">
      <div ref="percentage" class="percentage">{{ percentage }}%</div>
      <div ref="barBg" class="bar-bg" />
      <div ref="barActive" class="bar-active" />
      <div ref="pointer" class="pointer" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      countString: '0000000.0',
      count: 1201028.4,
      nowCount: 0,
      percentage: 0
    }
  },
  mounted() {
    setInterval(() => {
      this.addAnimate(12345)
      this.percentageAnimate()
    }, 20)
  },
  methods: {
    addAnimate(step) {
      this.nowCount += step
      this.countString = this.nowCount < this.count ? this.nowCount + '.0' : this.count + ''
      this.countString = this.countString.padStart(9, '0')
    },
    percentageAnimate() {
      if (this.percentage < 87) {
        this.percentage++
        this.$refs.pointer.style.left = this.percentage + '%'
        this.$refs.percentage.style.left = this.percentage + '%'
        this.$refs.barActive.style.width = this.percentage + '%'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.shell-data {
  .item.flex {
    display: flex;
    justify-content: space-between;
  }
  .item {
    position: relative;
    margin-top: 20px;
    .unit {
      color: #73aae5;
    }

    .card {
      font-family: Microsoft Yahei;
      font-size: 55px;
      font-weight: 700;
      color: #fff;
      margin-left: 3px;
      margin-right: 3px;
      box-sizing: content-box;
      width: 10%;
      text-align: center;
      background-color: #0f396b;
    }

    .card:nth-last-child(2) {
      background-color: transparent;
    }

    .percentage {
      position: absolute;
      top: -30px;
      left: 0px;
      font-size: 20px;
      font-weight: 700;
      color: #fff;
    }

    .bar-bg {
      margin: 0 auto;
      margin-top: 30px;
      width: 100%;
      height: 10px;
      border-radius: 5px;
      background-color: #283052;
    }

    .bar-active {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 10;
      border-radius: 5px;
      height: 10px;
      background: linear-gradient(90deg, #24cbff 30%, #9000ff);
    }

    .pointer {
      position: absolute;
      top: 15px;
      margin-left: -10px;
      width: 0;
      height: 0;
      border-top: 0 solid transparent;
      border-right: 10px solid transparent;
      border-left: 10px solid transparent;
      border-bottom: 10px solid #fff;
    }
  }
}
</style>
